//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ExerciseAdapter.h"
using namespace Cephei::QL::Models::Marketmodels::Products::Multistep;
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
#include <gen/QL/Models/Marketmodels/Callability/MarketModelExerciseValue.h>
#include <gen/QL/Models/Marketmodels/CurveState.h>
#include <gen/QL/Models/Marketmodels/MarketModelMultiProductCashFlow.h>
#include <gen/QL/Models/Marketmodels/Products/MultiProductMultiStep.h>
using namespace Cephei::QL::Models::Marketmodels;
using namespace Cephei::QL::Models::Marketmodels::Callability;
using namespace Cephei::QL::Models::Marketmodels::Products;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Products::Multistep::CExerciseAdapter::CExerciseAdapter (boost::shared_ptr<QuantLib::ExerciseAdapter>& childNative, Object^ owner) : CMultiProductMultiStep(CExerciseAdapter::typeid)
{
#ifdef HANDLE
	_phExerciseAdapter = NULL;
#endif
	_ppExerciseAdapter = &childNative;
    _ppMultiProductMultiStep = new boost::shared_ptr<QuantLib::MultiProductMultiStep> (boost::dynamic_pointer_cast<QuantLib::MultiProductMultiStep> (*_ppExerciseAdapter));
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CExerciseAdapter::CExerciseAdapter (QuantLib::ExerciseAdapter& childNative, Object^ owner) : CMultiProductMultiStep(CExerciseAdapter::typeid)
{
#ifdef HANDLE
	_phExerciseAdapter = NULL;
#endif
	_ppExerciseAdapter = new boost::shared_ptr<QuantLib::ExerciseAdapter> (&childNative);
    _ppMultiProductMultiStep = new boost::shared_ptr<QuantLib::MultiProductMultiStep> (boost::dynamic_pointer_cast<QuantLib::MultiProductMultiStep> (*_ppExerciseAdapter));
    _ExerciseAdapterOwner = owner;
    _MultiProductMultiStepOwner = owner;
}

Cephei::QL::Models::Marketmodels::Products::Multistep::CExerciseAdapter::CExerciseAdapter (CExerciseAdapter^ copy) : CMultiProductMultiStep(CExerciseAdapter::typeid)
{
#ifdef HANDLE
	_phExerciseAdapter = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppExerciseAdapter = new boost::shared_ptr<QuantLib::ExerciseAdapter> (copy->GetShared());
        _ppMultiProductMultiStep = new boost::shared_ptr<QuantLib::MultiProductMultiStep> (boost::dynamic_pointer_cast<QuantLib::MultiProductMultiStep> (*_ppExerciseAdapter));
    }
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CExerciseAdapter::CExerciseAdapter (PLATFORM::Type^ t) : CMultiProductMultiStep(CExerciseAdapter::typeid)
{
#ifdef HANDLE
	_phExerciseAdapter = NULL;
#endif
	if (!t->IsSubclassOf(CExerciseAdapter::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Products::Multistep::CExerciseAdapter::CExerciseAdapter (QuantLib::Handle<QuantLib::ExerciseAdapter>& childNative, Object^ owner)  : CMultiProductMultiStep(CExerciseAdapter::typeid)
{
	_phExerciseAdapter = &childNative;
	_ppExerciseAdapter = &static_cast<boost::shared_ptr<QuantLib::ExerciseAdapter>>(childNative.currentLink());
    _ppMultiProductMultiStep = new boost::shared_ptr<QuantLib::MultiProductMultiStep> (boost::dynamic_pointer_cast<QuantLib::MultiProductMultiStep> (*_ppExerciseAdapter));
    _ExerciseAdapterOwner = owner;
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CExerciseAdapter::CExerciseAdapter (QuantLib::Handle<QuantLib::ExerciseAdapter> childNative)  : CMultiProductMultiStep(CExerciseAdapter::typeid)
{
	_phExerciseAdapter = &childNative;
	_ppExerciseAdapter = &static_cast<boost::shared_ptr<QuantLib::ExerciseAdapter>>(childNative.currentLink());
    _ppMultiProductMultiStep = new boost::shared_ptr<QuantLib::MultiProductMultiStep> (boost::dynamic_pointer_cast<QuantLib::MultiProductMultiStep> (*_ppExerciseAdapter));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Products::Multistep::CExerciseAdapter::CExerciseAdapter (QuantLib::ExerciseAdapter childNative)  : CMultiProductMultiStep(CExerciseAdapter::typeid)
{
#ifdef HANDLE
	_phExerciseAdapter = NULL;
#endif
	_ppExerciseAdapter = new boost::shared_ptr<QuantLib::ExerciseAdapter> (new QuantLib::ExerciseAdapter (childNative));
    _ppMultiProductMultiStep = new boost::shared_ptr<QuantLib::MultiProductMultiStep> (boost::dynamic_pointer_cast<QuantLib::MultiProductMultiStep> (*_ppExerciseAdapter));
}
#endif

Cephei::QL::Models::Marketmodels::Products::Multistep::CExerciseAdapter::~CExerciseAdapter ()
{
    if (_ppExerciseAdapter != NULL)
    {
	    delete _ppExerciseAdapter;
        _ppExerciseAdapter = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Products::Multistep::CExerciseAdapter::!CExerciseAdapter ()
{
    if (_ppExerciseAdapter != NULL)
    {
	    delete _ppExerciseAdapter;
    }
}
QuantLib::ExerciseAdapter& Cephei::QL::Models::Marketmodels::Products::Multistep::CExerciseAdapter::GetReference ()
{
    if (_ppExerciseAdapter == NULL) throw REFNEW NativeNullException ();
	return **_ppExerciseAdapter;
}
boost::shared_ptr<QuantLib::ExerciseAdapter>& Cephei::QL::Models::Marketmodels::Products::Multistep::CExerciseAdapter::GetShared ()
{
    if (_ppExerciseAdapter == NULL) throw REFNEW NativeNullException ();
	return *_ppExerciseAdapter;
}
QuantLib::ExerciseAdapter* Cephei::QL::Models::Marketmodels::Products::Multistep::CExerciseAdapter::GetPointer ()
{
    if (_ppExerciseAdapter == NULL) throw REFNEW NativeNullException ();
	return &**_ppExerciseAdapter;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ExerciseAdapter>& Cephei::QL::Models::Marketmodels::Products::Multistep::CExerciseAdapter::GetHandle ()
{
	if (_phExerciseAdapter == NULL)
	{
		_phExerciseAdapter = new Handle<QuantLib::ExerciseAdapter> (*_ppExerciseAdapter);
	}
	return *_phExerciseAdapter;
}
#endif
bool Cephei::QL::Models::Marketmodels::Products::Multistep::CExerciseAdapter::HasNative () 
{
	return (_ppExerciseAdapter != NULL);
}

Cephei::QL::Models::Marketmodels::IEvolutionDescription^ Cephei::QL::Models::Marketmodels::Products::Multistep::CExerciseAdapter::Evolution::get ()
{
    try
    {
    	QuantLib::EvolutionDescription& _rv = (QuantLib::EvolutionDescription&)(*_ppExerciseAdapter)->evolution ( );   
        Cephei::QL::Models::Marketmodels::CEvolutionDescription^ _nrv = REFNEW Cephei::QL::Models::Marketmodels::CEvolutionDescription (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Callability::IMarketModelExerciseValue^ Cephei::QL::Models::Marketmodels::Products::Multistep::CExerciseAdapter::ExerciseValue::get ()
{
    try
    {
    	QuantLib::MarketModelExerciseValue& _rv = (QuantLib::MarketModelExerciseValue&)(*_ppExerciseAdapter)->exerciseValue ( );   
        Cephei::QL::Models::Marketmodels::Callability::CMarketModelExerciseValue^ _nrv = REFNEW Cephei::QL::Models::Marketmodels::Callability::CMarketModelExerciseValue (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Products::Multistep::CExerciseAdapter::MaxNumberOfCashFlowsPerProductPerStep::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppExerciseAdapter)->maxNumberOfCashFlowsPerProductPerStep ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Products::Multistep::CExerciseAdapter::NumberOfProducts::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppExerciseAdapter)->numberOfProducts ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::Products::Multistep::CExerciseAdapter::PossibleCashFlowTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time> _rv = (std::vector<QuantLib::Time>)(*_ppExerciseAdapter)->possibleCashFlowTimes ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Products::Multistep::IExerciseAdapter^ Cephei::QL::Models::Marketmodels::Products::Multistep::CExerciseAdapter::Reset::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppExerciseAdapter)->reset ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Models::Marketmodels::Products::Multistep::CExerciseAdapter::NextTimeStep (Cephei::QL::Models::Marketmodels::ICurveState^ prm1, Cephei::Core::IVector<UInt64>^ prm2, Cephei::Core::IMatrix<Cephei::QL::Models::Marketmodels::IMarketModelMultiProductCashFlow^>^ prm3)
{
    CCurveState^ _Cprm1;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cprm1 = safe_cast<CCurveState^> (prm1);
        _Cprm1->Lock();
        QuantLib::CurveState& _prm1 = static_cast<QuantLib::CurveState&> (_Cprm1->GetReference ()); 
        prm2->Lock();
        INativeVector<UInt64>^ _NCIprm2 = prm2->getFeature (NativeFeature::Value);
        CUInt64Vector^ _NCprm2 = safe_cast<CUInt64Vector^>(_NCIprm2);
        std::vector<QuantLib::Size>& _prm2 = static_cast<std::vector<QuantLib::Size>&> (_NCprm2->GetReference ());
        prm3->Lock();
        INativeMatrix<Cephei::QL::Models::Marketmodels::IMarketModelMultiProductCashFlow^>^ _NCIprm3 = prm3->getFeature (NativeFeature::Value);
        CMarketModelMultiProductCashFlowMatrix^ _NCprm3 = safe_cast<CMarketModelMultiProductCashFlowMatrix^>(_NCIprm3);
        std::vector<std::vector<QuantLib::MarketModelMultiProductCashFlow> >& _prm3 = static_cast<std::vector<std::vector<QuantLib::MarketModelMultiProductCashFlow> >&> (_NCprm3->GetReference ());
    	bool _rv = (bool)(*_ppExerciseAdapter)->nextTimeStep ( _prm1,  _prm2,  _prm3 );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
        if (prm2 != nullptr) prm2->Unlock(); //NO2  
        if (prm3 != nullptr) prm3->Unlock(); //NO2  
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

